package com.td.erhome.ui.sellhouse;

import android.app.Dialog;
import android.content.Intent;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.adapter.WritrBookAdapter;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.BaseBean;
import com.td.erhome.models.CiyiInfoBean;
import com.td.erhome.models.FloorBean;
import com.td.erhome.models.HouseArea;
import com.td.erhome.models.HouseBean;
import com.td.erhome.models.KeyStribgBean;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;
import com.td.erhome.utils.ConstUtils;
import com.td.erhome.utils.DialogUtils;
import com.td.erhome.utils.JsonParser;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.T;
import com.td.erhome.utils.TextUtil;
import com.td.erhome.utils.TypeUtils;
import com.td.erhome.utils.UserManager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;
import me.shaohui.advancedluban.Luban;
import me.shaohui.advancedluban.OnMultiCompressListener;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.td.erhome.R.id.tv_cx;
import static com.td.erhome.R.id.tv_dk;

public class EditHouseActivity extends BaseActivity implements WritrBookAdapter.OnRecyclerViewItemClickListener{

    @Bind(R.id.img_sure)
    TextView mImgSure;
    @Bind(R.id.lin_hx)
    LinearLayout mLinhx;
    @Bind(R.id.tv_hx)
    TextView mTvHx;
    @Bind(R.id.et_house_name)
    EditText mEtHouseName;
    @Bind(R.id.et_acreage)
    EditText mEtAcreage;
    @Bind(R.id.et_price)
    EditText mEtPrice;
    @Bind(R.id.et_name)
    EditText mEtName;
    @Bind(R.id.et_phone)
    TextView mEtPhone;
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.tv_xqdz)
    TextView mTvXqdz;
    @Bind(R.id.tv_lc)
    TextView mTvLc;
    @Bind(R.id.lin_lc)
    LinearLayout mLinLc;
    @Bind(R.id.tv_zx)
    TextView mTvZx;
    @Bind(R.id.tv_wy)
    TextView mTvWy;
    @Bind(tv_dk)
    TextView mTvDk;
    @Bind(tv_cx)
    TextView mTvCx;
    @Bind(R.id.lin_zx)
    LinearLayout mLinZx;
    @Bind(R.id.lin_cx)
    LinearLayout mLinCx;
    @Bind(R.id.lin_dk)
    LinearLayout mLinDk;
    @Bind(R.id.lin_wy)
    LinearLayout mLinWy;
    @Bind(R.id.lin_pt)
    LinearLayout mLinPt;
    @Bind(R.id.lin_time)
    LinearLayout mLinTime;
    @Bind(R.id.recyclview)
    RecyclerView mRecycview;
    @Bind(R.id.tv_pt)
    TextView mTvPt;
    List<String> data = new ArrayList<>();
    @Bind(R.id.et_building_num)
    EditText mEtBuildingNum;
    @Bind(R.id.et_building_unit)
    EditText mEtBuildingUnit;
    @Bind(R.id.et_house_num)
    EditText mEtHouseNum;
    @Bind(R.id.lin_zl)
    LinearLayout mLinZl;
    @Bind(R.id.tv_time)
    TextView mTvTime;
    @Bind(R.id.tv_pic_num)
    TextView mTvPicNum;
    @Bind(R.id.delet_house)
    TextView mDeleHouse;
    @Bind(R.id.Coordinator)
    CoordinatorLayout mCoordinator;
    @Bind(R.id.tv_mark)
    TextView mTvMark;
    @Bind(R.id.lin_js)
    RelativeLayout mLinJs;
    @Bind(R.id.img_is_accept_loan)
    ImageView mImgIsAcceptLoan;

    private String type_of_rooms;
    private String type_of_hall;
    private String type_of_guards;
    private String mArea_id;
    private String mPointLc;
    private String mHotelLc;
    private String mSplitStr;
    private DialogUtils mDialogUtils;
    private Subscription mSubscribe1;
    private Subscription mSubscribe2;
    private WritrBookAdapter mAdapter;
    private Subscription mSubscribe3;
    private Subscription mSubscribe4;
    private String mHouse_id;
    private ArrayList<String> paths = new ArrayList<>();
    private HouseBean.SellHouseListBean mSellHouseListBean;
    private List<String> mImgList = new ArrayList<>();
    private List<HouseBean.SellHouseListBean.HouseImageListBean> mHouse_image_list;
    private StringBuffer mBuffer;
    private List<File> mFileList= new ArrayList<>();
    private List<RequestBody> mRequestBodies= new ArrayList<>();
    int num = 0;
    private CiyiInfoBean mCiyiInfoBean;
    private FloorBean mFloorBean;
    private String is_accept_loan;
    private Dialog mDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_house;
    }

    @Override
    protected void initView() {
        mBuffer = new StringBuffer();
        mSellHouseListBean = (HouseBean.SellHouseListBean) getIntent().getSerializableExtra("SellHouseListBean");
        initData();
        mSubscribe1 = RxBus.getInstance().toObserverable(HouseArea.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HouseArea>() {
                    @Override
                    public void call(HouseArea houseArea) {
                        type_of_rooms = houseArea.type_of_rooms.substring(0,houseArea.type_of_rooms.length()-1);
                        type_of_hall = houseArea.type_of_hall.substring(0,houseArea.type_of_hall.length()-1);
                        type_of_guards = houseArea.type_of_guards.substring(0,houseArea.type_of_guards.length()-1);

                    }
                });

        mSubscribe2 = RxBus.getInstance().toObserverable(CiyiInfoBean.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CiyiInfoBean>() {
                    @Override
                    public void call(CiyiInfoBean ciyiInfoBean) {
                        mCiyiInfoBean = ciyiInfoBean;
                        mTvXqdz.setText(ciyiInfoBean.aeraLitePal.area_name+"   "+ciyiInfoBean.streetLitePal.area_name);
                        mArea_id = ciyiInfoBean.streetLitePal.getArea_id()+"";
                    }
                });

        mSubscribe3 = RxBus.getInstance().toObserverable(FloorBean.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<FloorBean>() {
                    @Override
                    public void call(FloorBean floorBean) {
                        mFloorBean = floorBean;
                        mTvLc.setText("第"+floorBean.pointLc+"层--共"+floorBean.hotelLc+"层");
                        mPointLc = floorBean.pointLc;
                        mHotelLc = floorBean.hotelLc;
                    }
                });

        mSubscribe4 = RxBus.getInstance().toObserverable(KeyStribgBean.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<KeyStribgBean>() {
                    @Override
                    public void call(KeyStribgBean keyBean) {
                        String[] split = keyBean.key.split(" ");
                        //配套设施
                        mSplitStr = JsonParser.serializeToJson(split);

                    }
                });

        mDialogUtils = new DialogUtils();
    }

    private void setFooterView(RecyclerView view) {
        View header = LayoutInflater.from(this).inflate(R.layout.write_book_header, view, false);
        ImageView mImgAdd = (ImageView) header.findViewById(R.id.img_add);
        mImgAdd.setOnClickListener(new FootClickListener());
        mAdapter.setFooterView(header);
    }

    @Override
    protected void releaseResource() {
        if (!mSubscribe1.isUnsubscribed()) {
            mSubscribe1.unsubscribe();
        }if (!mSubscribe2.isUnsubscribed()) {
            mSubscribe2.unsubscribe();
        }if (!mSubscribe3.isUnsubscribed()){
            mSubscribe3.unsubscribe();
        }if (!mSubscribe4.isUnsubscribed()){
            mSubscribe4.unsubscribe();
        }
        mDialogUtils = null;
    }

    public void initData(){
        mEtHouseName.setText(mSellHouseListBean.district_name);
        mTvXqdz.setText(mSellHouseListBean.area_name +"   "+mSellHouseListBean.street_name);
        mTvHx.setText(mSellHouseListBean.type_of_rooms+"室"+mSellHouseListBean.type_of_hall+"厅"+mSellHouseListBean.type_of_guards+"卫");
        mEtAcreage.setText(mSellHouseListBean.house_dimensions);
        mEtPrice.setText(mSellHouseListBean.sell_amount);
        mEtBuildingNum.setText(mSellHouseListBean.building_no);
        mEtBuildingUnit.setText(mSellHouseListBean.suite_no);
        mEtHouseNum.setText(mSellHouseListBean.house_no);
        mTvLc.setText("第"+mSellHouseListBean.house_floor+"层--总共"+mSellHouseListBean.total_floor+"层");
        mTvZx.setText(mSellHouseListBean.house_decoration_type);
        mTvCx.setText(mSellHouseListBean.orientation_of_house);
        mTvDk.setText(mSellHouseListBean.is_accept_buyer_loan);
        mTvWy.setText(mSellHouseListBean.is_only_housing);
        mTvTime.setText(mSellHouseListBean.house_buy_time);
        mEtName.setText(mSellHouseListBean.true_name);
        mEtPhone.setText(mSellHouseListBean.mobile);
        mTvMark.setText(mSellHouseListBean.house_owner_remark);
        mTvPt.setText(mSellHouseListBean.ancillary_facilities_name.toString().substring(1,mSellHouseListBean.ancillary_facilities_name.toString().length()-1));
        mArea_id = mSellHouseListBean.street_id;
        type_of_rooms = mSellHouseListBean.type_of_rooms;
        type_of_hall = mSellHouseListBean.type_of_hall;
        type_of_guards = mSellHouseListBean.type_of_guards;
        mHouse_id = mSellHouseListBean.id;
        mPointLc = mSellHouseListBean.house_floor;
        mHotelLc = mSellHouseListBean.total_floor;
        mHouse_image_list = mSellHouseListBean.house_image_list;
        mSplitStr = mSellHouseListBean.ancillary_facilities.toString();

        is_accept_loan = mSellHouseListBean.is_accept_buyer_loan;

        if (is_accept_loan.equals("2")){
            mImgIsAcceptLoan.setImageResource(R.mipmap.sellers_issue_icon_default);
        }else {
            mImgIsAcceptLoan.setImageResource(R.mipmap.sellers_issue_icon_selected);
        }


        for (int i = 0; i < mHouse_image_list.size() ; i++) {
            mImgList.add(mHouse_image_list.get(i).house_image);
        }

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        mRecycview.setLayoutManager(gridLayoutManager);
        mAdapter = new WritrBookAdapter(this,mImgList);
        mRecycview.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        setFooterView(mRecycview);

    }

    @OnClick({R.id.lin_hx, R.id.img_sure,R.id.back,R.id.tv_xqdz,R.id.lin_lc, R.id.lin_zx, R.id.lin_cx,
            R.id.lin_dk, R.id.lin_wy, R.id.lin_pt, R.id.lin_time,R.id.delet_house,
            R.id.tv_mark,R.id.lin_js})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lin_hx:
                mDialogUtils.createHomeBottom(this, mTvHx);
                break;
            case R.id.img_sure: //确定
                edihouse();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.tv_xqdz:
                mDialogUtils.createAddressBottom(this,mTvXqdz);
                break;
            case R.id.lin_lc: //楼层
                mDialogUtils.createlcBottom(this, mTvLc,mFloorBean);
                break;
            case R.id.lin_zx: //装修情况
                data.clear();
                data.add("毛坯房");
                data.add("简装");
                data.add("中装");
                data.add("精装");
                data.add("豪装");
                mDialogUtils.createBottom(this, data, mTvZx);
                break;
            case R.id.lin_cx: //房屋朝向
                data.clear();
                data.add("南");
                data.add("东");
                data.add("西");
                data.add("北");
                data.add("东南");
                data.add("西南");
                data.add("东北");
                data.add("西北");
                data.add("南北通透");
                data.add("东西通透");
                mDialogUtils.createBottom(this, data, mTvCx);
                break;
            case R.id.lin_dk: //是否有贷款
                data.clear();
                data.add("是");
                data.add("否");
                mDialogUtils.createBottom(this, data, mTvDk);
                break;
            case R.id.lin_wy: //是否唯一住房
                data.clear();
                data.add("是");
                data.add("否");
                mDialogUtils.createBottom(this, data, mTvWy);
                break;
            case R.id.lin_pt:
                mDialogUtils.createCheckBox(this,mTvPt);
                break;
            case R.id.lin_time:
                mDialogUtils.createTime(this,mTvTime);
                break;
            case R.id.delet_house:
                Snackbar.make(mCoordinator, "确定删除该房源?",Snackbar.LENGTH_LONG).setAction("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deletHouse();
                    }
                }).show();
                break;
            case R.id.tv_mark:
                Intent intent = new Intent(this,MarkActivity.class);
                startActivityForResult(intent,201);
                break;
            case R.id.lin_js:
                if (is_accept_loan.equals("2")){
                    mImgIsAcceptLoan.setImageResource(R.mipmap.sellers_issue_icon_selected);
                    is_accept_loan ="1";
                }else {
                    mImgIsAcceptLoan.setImageResource(R.mipmap.sellers_issue_icon_default);
                    is_accept_loan ="2";
                }
                break;
        }
    }

   /* @Override
    public void onBackPressed() {
        if (paths.size() == 0) {
            finish();
        } else if (mAdapter.isShow) {
            mAdapter.isShow = false;
            mAdapter.notifyDataSetChanged();
        } else {
            paths.clear();
            mAdapter.addData(paths);
        }

    }*/
    @Override
    public void onItemClick(int point) {
        if (mHouse_image_list.size()<=point){
            mImgList.remove(point);
            mFileList.remove(point-mHouse_image_list.size());
        }else {
            mImgList.remove(point);
            mBuffer.append(mHouse_image_list.get(point).house_image_id+",");
            mHouse_image_list.remove(point);
        }
        mAdapter.notifyDataSetChanged();
    }

    /** 修改房源
     */
    private void edihouse() {

        if (TextUtil.isEditTextEmpty(mEtHouseName)) {
            T.showLong(this, "请输入小区名称");
            return;
        }
        if (TextUtil.isTextViewEmpty(mTvXqdz)) {
            T.showLong(this, "请选择小区地址");
            return;
        }
        if (TextUtil.isTextViewEmpty(mTvHx)) {
            T.showLong(this, "请选择房屋户型");
            return;
        }

        if (TextUtil.isTextViewEmpty(mEtAcreage)) {
            T.showLong(this, "请输入房屋面积");
            return;
        }
        if (TextUtil.isEditTextEmpty(mEtPrice)) {
            T.showLong(this, "请输入房屋售价");
            return;
        }
        if (TextUtil.isEditTextEmpty(mEtName)) {
            T.showLong(this, "请输入屋主姓名");
            return;
        }
        if (TextUtil.isTextViewEmpty(mEtPhone)) {
            T.showLong(this, "请输入联系电话");
            return;
        }
        compressMultiListener();
        }

    /**
     * 删除房源
     */
   private void deletHouse(){
       HashMap<String, Object> params = new HashMap<>();
       params.put("user_id", UserManager.getIns().getUser().uid+"");
       params.put("house_id", mSellHouseListBean.id);

       Api.getRetrofit().deleteSellHouse(params)
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new BaseSubscriber<BaseBean>(EditHouseActivity.this){
                   @Override
                   public void onNext(BaseBean baseBean) {
                       super.onNext(baseBean);
                       RxBus.getInstance().post(ConstUtils.DE_HOUSE);
                       T.showLong(EditHouseActivity.this,"删除房源成功");
                       finish();
                   }
               });
   }

    /**
     * 压缩单张图片 Listener 方式
     */

    private void compressMultiListener() {

        if (mAdapter.mPaths.size()!=6) {
            T.showLong(this,"请上传6张图片");
            return;
        }

        if (mFileList.size()==0) {
            postData(mFileList, mRequestBodies);
            return;
        }
        Luban.compress(this, mFileList)
                .putGear(Luban.FIRST_GEAR)
                .launch(new OnMultiCompressListener() {
                    @Override
                    public void onStart() {
                        if (mDialog ==null){
                            mDialog = new DialogUtils().loading(EditHouseActivity.this);
                        }
                        mDialog.show();
                    }

                    @Override
                    public void onSuccess(List<File> fileList) {
                        int size = fileList.size();
                        while (size-- > 0) {
                            RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), fileList.get(size));
                            mRequestBodies.add(fileBody);
                        }

                        postData(mFileList, mRequestBodies);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }
                });
    }



    public void postData(List<File> fileList,List<RequestBody> requestBodies){
       MultipartBody boy = null;
       if (fileList.size() == 0){
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .build();
       }
       else if (fileList.size() == 1) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName()+"a", requestBodies.get(0))
                   .build();
       } else if (fileList.size() == 2) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼好
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName()+"a", requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName()+"b", requestBodies.get(1))
                   .build();
       } else if (fileList.size() == 3) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName()+"a", requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName()+"b", requestBodies.get(1))
                   .addFormDataPart("house_img[]", fileList.get(2).getName()+"c", requestBodies.get(2))
                   .build();
       } else if (fileList.size() == 4) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName()+"a", requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName()+"b", requestBodies.get(1))
                   .addFormDataPart("house_img[]", fileList.get(2).getName()+"c", requestBodies.get(2))
                   .addFormDataPart("house_img[]", fileList.get(3).getName()+"d", requestBodies.get(3))
                   .build();
       } else if (fileList.size() == 5) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                   .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                   .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                   .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                   .build();
       } else if (fileList.size() == 6) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                   .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                   .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                   .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                   .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                   .build();
       } else if (fileList.size() == 7) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                   .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                   .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                   .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                   .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                   .addFormDataPart("house_img[]", fileList.get(6).getName(), requestBodies.get(6))
                   .build();
       } else if (fileList.size() == 8) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                   .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                   .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                   .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                   .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                   .addFormDataPart("house_img[]", fileList.get(6).getName(), requestBodies.get(6))
                   .addFormDataPart("house_img[]", fileList.get(7).getName(), requestBodies.get(7))
                   .build();
       } else if (fileList.size() == 9) {
           boy = new MultipartBody.Builder()
                   .setType(MultipartBody.FORM)
                   .addFormDataPart("community_name", mEtHouseName.getText().toString())
                   .addFormDataPart("street_id", mArea_id+"")
                   .addFormDataPart("house_area", mEtAcreage.getText().toString())
                   .addFormDataPart("house_price", mEtPrice.getText().toString())
                   .addFormDataPart("user_name", mEtName.getText().toString())
                   .addFormDataPart("user_tel", mEtPhone.getText().toString())
                   .addFormDataPart("type_of_rooms", this.type_of_rooms)
                   .addFormDataPart("type_of_hall", type_of_hall)
                   .addFormDataPart("type_of_guards", type_of_guards)
                   .addFormDataPart("del_house_img_id", mBuffer.toString())
                   .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                   .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                   .addFormDataPart("house_id", mHouse_id)
                   .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                   .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                   .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                   .addFormDataPart("house_floor", mPointLc) //楼层
                   .addFormDataPart("total_floor", mHotelLc) //总楼层
                   .addFormDataPart("is_accept_loan", is_accept_loan)
                   .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                   .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                   .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                   .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                   .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                   .addFormDataPart("facilities", mSplitStr) //配套设施
                   .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                   .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                   .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                   .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                   .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                   .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                   .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                   .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                   .addFormDataPart("house_img[]", fileList.get(6).getName(), requestBodies.get(6))
                   .addFormDataPart("house_img[]", fileList.get(7).getName(), requestBodies.get(7))
                   .addFormDataPart("house_img[]", fileList.get(8).getName(), requestBodies.get(8))
                   .build();
       }

       Api.getRetrofit().editSellHouse(boy)
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new BaseSubscriber<BaseBean>(EditHouseActivity.this){
                   @Override
                   public void onStart() {
                   }

                   @Override
                   public void onCompleted() {
                       if (mDialog!=null && mDialog.isShowing()){
                           mDialog.dismiss();
                       }
                   }

                   @Override
                   public void onError(Throwable e) {
                       if (mDialog!=null && mDialog.isShowing()){
                           mDialog.dismiss();
                       }
                       T.showLong(EditHouseActivity.this,e.getMessage());
                   }

                   @Override
                   public void onNext(BaseBean baseBean) {
                       super.onNext(baseBean);
                       RxBus.getInstance().post(ConstUtils.DE_HOUSE);
                       T.showLong(EditHouseActivity.this,"修改成功");

                       if (mDialog!=null && mDialog.isShowing()){
                           mDialog.dismiss();
                       }
                   }
               });
   }

    //进入选择图片
    private class FootClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            if (mAdapter.mPaths.size()==6){
                T.showLong(EditHouseActivity.this,"只能上传6张图片,重新选择请先长按图片删除");
                return;
            }

            mAdapter.isShow = false;
            mAdapter.notifyDataSetChanged();
            PhotoPicker.builder()
                    .setPhotoCount(6-mAdapter.mPaths.size())
                    .start(EditHouseActivity.this);
        }
    }

    /**
     * 图片选择回调
     */

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK &&
                (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {
            if (data != null) {

                mFileList.clear();
                mRequestBodies.clear();
                paths.clear();

                paths = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);

                mAdapter.addAllData(paths);
                for (String str : paths) {
                    mFileList.add(new File(str));
                }

            }
        }

        if (requestCode==201&&resultCode == RESULT_OK){
            mTvMark.setText(data.getStringExtra("mark"));
        }

    }

}
